unit database_cevs;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  EditBtn, database_base, itemselectorframeunit, eclistframeunit, fpjson;

type

  { TDatabaseCevsFrame }

  TDatabaseCevsFrame = class(TDatabaseBaseFrame)
    CevSelectorFrame: TItemSelectorFrame;
    CommandListGroupBox: TGroupBox;
    CevECListFrame: TECListFrame;
    TriggerComboBox: TComboBox;
    SwitchEditButton: TEditButton;
    NameEdit: TEdit;
    GeneralSettingsGroupBox: TGroupBox;
    NameLabel: TLabel;
    MainContentPanel: TPanel;
    TriggerLabel: TLabel;
    SwitchLabel: TLabel;
    NamePanel: TPanel;
    TriggerPanel: TPanel;
    SwitchNamePanel: TPanel;
    SelectorMainContentSplitter: TSplitter;

    procedure Init; override;
    procedure LoadCevData(NewId: Integer);
    function GetSelectedCEv: TJSONObject;
    procedure NameEditChange(Sender: TObject);
    procedure SwitchEditButtonButtonClick(Sender: TObject);
    procedure SwitchEditButtonKeyPress(Sender: TObject; var Key: char);
    procedure TriggerComboBoxChange(Sender: TObject);
  private
    CurrentId: Integer;
    IsLoading: Boolean;

    procedure UpdateSwitch; inline;
    procedure UpdateSwitchName(SwitchId: Integer); inline;

  public
    destructor Destroy; override;
  end;

resourcestring
  CevsHeader = 'Common Events';

implementation

uses
  comboboxhelper, namedindexselection;

{$R *.lfm}

{ TDatabaseCevsFrame }

procedure TDatabaseCevsFrame.Init;
begin
  IsLoading := True;
  CevSelectorFrame.SetData(Db.CommonEvents, @Db.ResizeCommonEvents, @LoadCevData);
  CevSelectorFrame.Title := CevsHeader;
  CevSelectorFrame.JsonDataType := 'application/rpgmv-commonEvents';
  CevSelectorFrame.CreateElementCallback := @Db.CreateEmptyCommonEvent{TODO};

  FillCEvTriggerComboBox(TriggerComboBox);
  CevECListFrame.SetDatabase(Db);

  LoadCevData(1);
end;

procedure TDatabaseCevsFrame.LoadCevData(NewId: Integer);
var
  CEv: TJSONObject = nil;
  CEvName: String = '';
  Trigger: Integer = 0;
  CEvNameJson: TJSONString = nil;
  TriggerJson: TJSONNumber = nil;
  List: TJSONArray = nil;
begin
  IsLoading := True;
  CurrentId := NewId;

  CEv := GetSelectedCEv;

  if (CEv <> nil) and CEv.Find('name', CEvNameJson) then
    CEvName := CEvNameJson.AsString;
  NameEdit.Text := CEvName;

  if (CEv <> nil) and CEv.Find('trigger', TriggerJson) then
    Trigger := TriggerJson.AsInteger;
  TriggerComboBox.ItemIndex := Trigger;

  UpdateSwitch;

  if (CEv <> nil) and CEv.Find('list', List) then
    CevECListFrame.SetList(List)
  else
    CevECListFrame.SetList(nil);

  IsLoading := False;
end;

function TDatabaseCevsFrame.GetSelectedCEv: TJSONObject;
begin
  GetSelectedCEv := Db.CommonEvents.Objects[CurrentId];
end;

procedure TDatabaseCevsFrame.NameEditChange(Sender: TObject);
var
  CEv: TJSONObject = nil;
begin
  if not IsLoading then
    CEv := GetSelectedCEv;

  if CEv <> nil then begin
    CEv.Strings['name'] := NameEdit.Text;
    CevSelectorFrame.RefreshSelectedName;
  end;
end;

procedure TDatabaseCevsFrame.SwitchEditButtonButtonClick(Sender: TObject);
var
  CEv: TJSONObject;
  NewId: Integer;
begin
  CEv := GetSelectedCEv;

  NewId := NamedIndexSelectionForm.SelectSwitch(CEv.Integers['switchId'], Db);
  CEv.Integers['switchId'] := NewId;
  UpdateSwitch
end;

procedure TDatabaseCevsFrame.SwitchEditButtonKeyPress(Sender: TObject;
  var Key: char);
begin
  SwitchEditButtonButtonClick(Sender);
  Key := #0;
end;

procedure TDatabaseCevsFrame.TriggerComboBoxChange(Sender: TObject);
var
  CEv: TJSONObject = nil;
begin
  if not IsLoading then
    CEv := GetSelectedCEv;

  if CEv <> nil then begin
    CEv.Integers['trigger'] := TriggerComboBox.ItemIndex;
    UpdateSwitch
  end;
end;

procedure TDatabaseCevsFrame.UpdateSwitch;
var
  CEv: TJSONObject = nil;
begin
  CEv := GetSelectedCEv;
  SwitchEditButton.Enabled := TriggerComboBox.ItemIndex > 0;
  UpdateSwitchName(CEv.Integers['switchId']);
end;

procedure TDatabaseCevsFrame.UpdateSwitchName(SwitchId: Integer);
begin
  if TriggerComboBox.ItemIndex = 0 then
    SwitchEditButton.Text := ''
  else
    SwitchEditButton.Text := Db.GetSwitchName(SwitchId);
end;

destructor TDatabaseCevsFrame.Destroy;
begin
  inherited Destroy;
end;

end.

